package com.app.service.impl;

import com.app.entity.Enums.TextTimeEnum;
import com.app.entity.Orders;
import com.app.entity.Subject;
import com.app.entity.User;
import com.app.entity.resParams.OrdersCoachRes;
import com.app.entity.resParams.UserOrders;
import com.app.mapper.OrderMapper;
import com.app.mapper.SubjectMapper;
import com.app.mapper.UserMapper;
import com.app.service.OrderService;
import com.app.util.Result;
import com.app.util.RoleUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2021-04-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Result seeStudent(Integer subid) {
        Orders orders = orderMapper.selectOne(new QueryWrapper<Orders>().eq("subid", subid));
        Integer sid = orders.getSid();//得到学员id
        return Result.success(userMapper.selectById(sid));
    }

    @Override
    public Result findOrdersAndCoach(Integer type) {
        Integer sid = RoleUtil.getRoleId(); //学生id
        String time = null;
        if (type == 0) { //查看近期的
            time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        List<OrdersCoachRes> list = orderMapper.findOrdersAndCoach(sid, time);
        return Result.success(list);
    }

    @Override
    public Result endPractice(Integer id) {
        Orders orders = orderMapper.selectOne(new QueryWrapper<Orders>().eq("subid", id));
        orders.setState('1');//设置状态为1
        orderMapper.updateById(orders);
        subjectMapper.updateById(new Subject().setId(id).setSigns('1'));
        return Result.success();
    }

    @Override
    public Result submitPinJia(Orders orders) {
        orders.setState('2');
        orderMapper.updateById(orders); //订单更新
        return Result.success();
    }

    @Override
    public Result findEvaluateByStudent() {
        Integer sid = RoleUtil.getRoleId(); //用户id
        List<Orders> list = orderMapper.selectList(new QueryWrapper<Orders>().eq("sid", sid).eq("state", '2'));
        return Result.success(list);
    }

    @Override
    public Result findSurplusHour() {
        Integer id = RoleUtil.getRoleId();
        User user = userMapper.selectById(id);
        if (user.getTestStatus() == '1') {
            return Result.fail();   //科一无法查询状态
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sid", id);
        queryWrapper.eq("state", '2'); //要评论评论完成的状态
        queryWrapper.eq("text_state", user.getTestStatus());
        Integer one = orderMapper.selectCount(queryWrapper);
        Integer value = getValue(user.getTestStatus());
        float a = (float) one / value;
        if (a >= 1) {
            a = 1;
        }
        return Result.success(a * 100);
    }

    @Override
    public Result findOrdersByCoach() {
        Integer cid = RoleUtil.getRoleId();//教练id
        //查看被预约和已经审核通过的
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<Subject>().eq("cid",cid).eq("state", '1').eq("signs", '1');
        List<Subject> list = subjectMapper.selectList(queryWrapper);
        if (list.size() == 0) {
            return Result.success();
        }
        List list1 = new ArrayList(list.size() + 1);
        list.forEach(item -> {
            UserOrders userOrders = orderMapper.findOrdersByCoach(item.getId());
            list1.add(userOrders);
        });
        return Result.success(list1);
    }

    @Override
    public Result findAllOrderByAdmin() {
        List<OrdersCoachRes> list = orderMapper.findAllOrderByAdmin('2'); //订单状态
        return Result.success(list);
    }

    public Integer getValue(Character code) {
        TextTimeEnum[] values = TextTimeEnum.values();
        for (TextTimeEnum timeEnum : values) {
            if (timeEnum.getCode().equals(code)) {
                return timeEnum.getValue();
            }
        }
        return 0;
    }
}
